package cn.com.cetccst.uicomponents.binding.recyclerview;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import cn.com.cetccst.uicomponents.binding.recyclerview.layoutmanager.WrapContentGridLayoutManager;
import cn.com.cetccst.uicomponents.binding.recyclerview.layoutmanager.WrapContentLinearLayoutManager;
import cn.com.cetccst.uicomponents.binding.recyclerview.layoutmanager.WrapContentStaggeredGridLayoutManager;

import java.util.List;

public class RecyclerViewBindingAdapter {

    @BindingAdapter(value = {"adapter", "submitList"}, requireAll = false)
    public static void bindList(final RecyclerView recyclerView, ListAdapter adapter, List list) {

        if (recyclerView.getAdapter() == null || adapter != recyclerView.getAdapter()) {
            recyclerView.setAdapter(adapter);
        }

        adapter.submitList(list);

        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
    }
    @BindingAdapter(value = "linearLayoutHorizontal")
    public static void bindLinearLayout(final RecyclerView recyclerView, final boolean linearLayoutHorizontal) {
        if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
            recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(recyclerView.getContext()));
            if (linearLayoutHorizontal) {
                ((LinearLayoutManager) recyclerView.getLayoutManager()).setOrientation(RecyclerView.HORIZONTAL);
            }

        }
    }
}
